<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\model;

/**
 * 模型
 */
class UserKingInvestment extends ModelBase
{
    protected $insert = ['create_time','trade_no'];
    protected $update = ['update_time'];
    //关联赌王产品表
    public function investment(){
        return $this->belongsTo('GamblerKingInvestment','investment_id','id');
    }


    //关联transfer表
    public function kinguser(){
        return $this->belongsTo('GamblerKingInvestmentUser','user_investment_id','id');
    }

    //关联member表
    public function member(){
        return $this->belongsTo('Member','user_id','id');
    }
    //关联赌王表
    public function king(){
        return $this->belongsTo('GamblerKing','king_id','id');
    }

    /**
     * 时间自动完成
     */
    public function setCreateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 时间自动完成
     */
    public function setUpdateTimeAttr()
    {

        return TIME_NOW;
    }

    /**
     * 获取状态信息
     */
    public function getStateTextAttr(){
        $arr=['委托成功','委托未付款','已解除委托','已取消委托','分红已完成','等待交易员接单','游戏进行中','委托交易员开始超时','委托失败'];
        return lang($arr[$this->state]);
    }


    /**
     * AI流水号自动完成
     */
    public function setTradeNoAttr()
    {

        return 'AI'.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8).rand(1000,9999);
    }

    /**
     * 时间自动完成
     */
    public function getCreateTimeAttr($value)
    {

        return date('Y-m-d H:i:s',$value);
    }

    /**
     * 解除委托
     */

    public function  cancel_order($remark=''){
        //修改状态为解除中
        $user_account=UserAccount::where(['user_id'=>$this->user_id,'wallet_id'=>3])->lock(true)->find();
        $member=\app\common\model\Member::get($this->user_id);

        $user_account->balance+=$this->money;
        $user_account->freeze_balance-=$this->money;

        //写入资金流水
        UserAccountLog::create([
            'user_id'=>$user_account['user_id'],
            'number'=>$this->money,
            'wallet_id'=>$user_account['wallet_id'],
            'before_blance'=>$user_account->balance-$this->money,
            'after_blance'=>$user_account->balance,
            'is_add'=>1,
            'remark'=>$remark,
            'type'=>8
        ]);

        $user_account->save();

    }



}
